************************************************
* Project - Do financial concerns make workers less productive?
* Researchers:  Supreet Kaur, Sendhil Mullainathan, Suanna Oh, Frank Schilbach
* Purpose: Main do-file. Runs a push-button replication of "Do financial concerns make workers less productive?"
* Last edited: 06 February 2023
************************************************
/* Instructions: .
	1. Set the path to this replication package in the areas marked "SET DIRECTORY HERE" (Lines 19 and 46 or 56)
	2. Set the globals "MHT", "reps", and "rsour"; the first takes 1 if you would like to replicate the multiple hypothesis testing table (A8) and 0 if not. The second sets the number of replications for the multiple hypothesis test corrections. The final takes 1 if you would like to run the r code that replicates Figure 1 as well, and 0 (or other) if not.
	3. Run this do-file 
*/
****************************************************************************
*** Step 1: setup
clear all
set more off

version 14

global dir 	"" //SET DIRECTORY HERE
 
global s "$dir/Scripts"
global sm "$dir/Scripts/Main"
global sa "$dir/Scripts/Appendix"
global om "$dir/Output/Main"
global oa "$dir/Output/Appendix"
global t "Tables"
global f "Figures"


global MHT = 0 //Keep at 1 if you would like to replicate the p-value adjustments from Table A8; change to 0 (or other) if not.
global reps = 2000 //Set the number of iterations for the MHT correction code; 2,000 are required to perfectly replicate the paper tables
global rsour = 0 //Set to 1 if you would like to run the R code (reproduces Figure 1) from this main do-file.

* Step 2: Installing ssc commands necessary to run some chunks
***** The user-installed pacakges are listed below -- the code checks whether they are installed and if not, installs them. pdslasso and rlasso are also parts of the larger "lassopack" ssc package, so if you are having trouble with them, try running "ssc install lassopack".
global packages estout pdslasso rlasso reghdfe ftools wyoung rsource
foreach package of global packages  {
		capture which `package'
		if _rc==111 ssc install `package'
}

* Step 3: Preparing the R environment. NOTE: the below will not run if you delete the lines in the if/else statements after "END_OF_R." Toggle global rsour to one above if you would like to run the r code from this main dofile
if $rsour == 1{
	if "`c(os)'"=="MacOSX" | "`c(os)'"=="UNIX" {
    rsource, terminator(END_OF_R) rpath("/usr/local/bin/R") roptions(`"--vanilla"')  maxlines(2)
	setwd(""); // SET DIRECTORY HERE
	q();
	END_OF_R
	
	
	
}

else {  // windows
    rsource terminator(END_OF_R) rpath("/usr/local/bin/R") roptions(`"--vanilla"')  maxlines(2)
	setwd(""); // SET DIRECTORY HERE
	q();
	END_OF_R
	
	
	
}
}

* Step 4: Running do-files

***** Generate variables for analysis and save output
do "$s/0_generate_variables.do"	

***** Running do-files for main paper tables *****
forvalues x=1/7{
	do "$sm/Table_`x'.do"
}

***** Running do-files for main paper figures *****
forvalues x=3/4{
	do "$sm/Figure_`x'.do"
}

***** Running do-files for Appendix tables *****
forvalues x=1/7{
	do "$sa/Table_A`x'.do"
}

forvalues x=9/17{
	do "$sa/Table_A`x'.do"
}

if $MHT == 1{
	do "$sa/Table_A8.do"
}

***** Running do-files for Appendix figures *****
do "$sa/Figure_A3.do"


* Step 5: Running R scripts:
if $rsour == 1{
cd "$dir"
if "`c(os)'"=="MacOSX" | "`c(os)'"=="UNIX" {
    rsource using Scripts/Main/Figure_1.R, rpath("/usr/local/bin/R") roptions(`"--vanilla"') //Figure 1

}
else {  // windows
    rsource using Scripts/Main/Figure_1.R, rpath(`"c:\r\R-3.5.1\bin\Rterm.exe"') roptions(`"--vanilla"')  // change version number, if necessary
}
}

